# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
pkgname=dns-root-hints
pkgver=2022062901
pkgrel=0
pkgdesc="The DNS root hint(s)"
url="https://www.internic.net/domain"
arch="noarch"
license="Public-Domain"
depends="curl gpgv"
makedepends="gpg"
source="verisign-grs-nstld-key.asc
	named.root
	named.root.sig
	update-$pkgname
	$pkgname.crond
	"
builddir="$srcdir"

prepare() {
	default_prepare

	# Convert the key to the GPG keyring that can be used with gpgv.
	gpg --dearmor -o verisign-grs-nstld-key.gpg verisign-grs-nstld-key.asc
}

package() {
	install -D -m 644 -t "$pkgdir"/usr/share/$pkgname \
		named.root \
		named.root.sig \
		verisign-grs-nstld-key.asc \
		verisign-grs-nstld-key.gpg

	install -D -m 755 -t "$pkgdir"/usr/bin/ update-$pkgname

	install -D -m 755 $pkgname.crond "$pkgdir"/etc/periodic/monthly/$pkgname

	# compatibility links
	cd "$pkgdir"/usr/share/$pkgname
	ln -s named.root named.cache
	ln -s named.root db.cache
}

check() {
	local awkprog='
		/related version of root zone:/ {
			rootver=$NF;
			if (pkgver != rootver) {
				$1="ERROR:";
				print;
				exit 1;
			};
			printf "OK: %s\n", rootver;
			quit;
		}'
	awk -v pkgver="$pkgver" "$awkprog" "$srcdir"/named.root
}

# check new versions of root hints and commit
snapshot() {
	local new_ver

	if ! [ -f "$srcdir"/verisign-grs-nstld-key.gpg ]; then
		abuild unpack
		abuild prepare
	fi

	DNS_ROOT_HINTS_DIR="$srcdir" ./update-dns-root-hints

	# commit if new version was found
	if ! cmp -s named.root "$srcdir"/named.root; then
		cp "$srcdir"/named.root* .
		git add named.root named.root.sig

		new_ver=$(sed -En 's/.*related version of root zone:\s*([0-9]{10}).*/\1/p' named.root)
		abump $pkgname-$new_ver
	fi
}

sha512sums="
3ecf5d66e506526ad98ea0b371202f0763b987322bd4407b40fcd95415202bddb18fd06c82eb397566b393e214dc88cb17ec94f3908328e8a55f5f68cc730993  verisign-grs-nstld-key.asc
9c838b409e8fc184cc33588eae5834e51f4632b6bde285b6b57b9db98e217481e1ae549de075ab883ae67e393fb0b73809f1703e1e13c4a521cc93137de22850  named.root
28e3b3b3b3b0a93ed5a6cba615a0f7f99f5b6b236c2f58013946ae7ecc008c2158bc253b5b251ee0e483e2cff5cc008fdd7c15af6a3d6b05ba35e0217023b9d1  named.root.sig
ba5663675e5b4cb0f670c570a4cef1fde8282185c5cc505af8beb7f32756a41ff511024f45c57c7204d03a88b8d2cc86ed31943ed19b71d30cee040d634a8208  update-dns-root-hints
68a743ee7b0f60eeb21cecbb916c3d7051ebbb86143621aafe5ef654df0953a274a2ffa2f3fe42fb3c143fd7b29ab4c5b17407c73b560a1c19afa1584fc43d8a  dns-root-hints.crond
"
